/**
 * Created by json on 2016/8/18.
 */
dwc.GX_MyButton = cc.Node.extend({
    m_clickRect : null,
    multiple: 10,
    ctor : function(_spriteImage,_width,_height){
        this._super();
        this.spriteImage=_spriteImage;
        this.width=_width;
        this.height=_height;
    },

    onEnter : function(){
        this._super();
        var that = this;
        this.btnSprite = new cc.Sprite(this.spriteImage);
        this.btnSprite.setAnchorPoint(0,0);
        this.btnSprite.setPosition(0,0);
        this.addChild(this.btnSprite);

        this.colorLayer = new cc.LayerColor(cc.color(77,77,77),this.width, this.height);
        this.colorLayer.setAnchorPoint(0,0);
        this.colorLayer.setPosition(0,0);
        this.colorLayer.setVisible(false);
        this.colorLayer.setOpacity(150);
        this.addChild(this.colorLayer);

        cc.eventManager.addListener({
            event : cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches : false,
            onTouchBegan : function(touch, event){ return that.onTouchBegan(touch, event); },
            onTouchMoved : function(touch, event){ that.onTouchMoved(touch, event); },
            onTouchEnded : function(touch, event){ that.onTouchEnded(touch, event); },
            onTouchCancelled : function(touch, event){ that.onTouchCancelled(touch, event); },
        }, this);
    },

    onExit : function(){
        this._super();
    },
    addClickEventListener: function(callback){
        this._clickEventListener = callback;
    },
    onTouchBegan : function(touch, event){

        var point =  touch.getLocation();
        if(this.m_clickRect != null && !cc.rectContainsPoint(this.m_clickRect, point)){
            return false;
        }

        point = this.convertToNodeSpace(point);
        var rect = cc.rect(0, 0, this.width, this.height);
        if(!cc.rectContainsPoint(rect, point)){
            return false;
        }

        this.m_beginPoint = this.convertToWorldSpace(this.getPosition());
        this._SetSelected(true);

        return true;
    },

    onTouchMoved : function(touch, event){
        var point =  touch.getLocation();
        if(this.m_clickRect != null && !cc.rectContainsPoint(this.m_clickRect, point)){
            this._SetSelected(false);
            return;
        }

        point = this.convertToNodeSpace(point);
        var rect = cc.rect(0, 0, this.width, this.height);
        if(!cc.rectContainsPoint(rect, point)){
            this._SetSelected(false);
            return;
        }

        var pos = this.convertToWorldSpace(this.getPosition());
        if(Math.abs(pos.y - this.m_beginPoint.y) > 50
            || Math.abs(pos.x - this.m_beginPoint.x) > 50){
            this._SetSelected(false);
            return;
        }
    },

    onTouchEnded : function(touch, event){

        this._SetSelected(false);

        var point =  touch.getLocation();
        point = this.convertToNodeSpace(point);
        var rect = cc.rect(0, 0, this.width, this.height);
        if(!cc.rectContainsPoint(rect, point)){
            return;
        }

        var pos = this.convertToWorldSpace(this.getPosition());
        if(Math.abs(pos.y - this.m_beginPoint.y) > 50
            || Math.abs(pos.x - this.m_beginPoint.x) > 50){
            return;
        }

        this.OnMyButtonClick();
    },

    onTouchCancelled : function(touch, event){
        this._SetSelected(false);
    },

    _SetSelected : function(__isSelected){
        this.colorLayer.setVisible(__isSelected);
    },

    OnMyButtonClick : function(){
        if(this._clickEventListener)
        {
            this._clickEventListener();
        }
    }
});

/*
 * 功能: 用户头像管理层--上传截图和下载保存头像
 * 作者: 张  宝
 * 时间: 2015-10-19
 *
 */

dwc.GX_UserHeadImgManager = {
    /***********************公共方法*****************************/
    //选择图片,参数:__userId,用户ID,number __callback(bool)回调函数
    ChooseUserHeadPic : function(__userId,__callback){
        //不支持html-5
        if (!cc.sys.isNative) { //true in jsb, false in browsers
            cc.assert("This sys not is Native! 不支持HTML-5.");
            return;
        }
        //参数判断
        if(typeof __userId != "number" || typeof __callback != "function"){
            cc.assert("获取参数不正确!");
            return;
        }

        var imgName = parseInt(__userId) + ".png";

        var self = this;
        //注册底层截图和保存的监听
        var l_listener4 = cc.EventListener.create({
            event    : cc.EventListener.CUSTOM,
            eventName: "IMAGE_UPDATE_START",
            callback : function(event){
                //保存修改信息
                var secends = parseInt(new Date().getTime() / 1000);//当前时间秒数
                cc.sys.localStorage.setItem(imgName, secends + "");
                //调用回调
                __callback && __callback(true);
            }
        });
        cc.eventManager.addListener(l_listener4, this);

        //调用不用平台底层界面
        dwc.NativeWrapper.ShowChooseView();
    },

    GetUrlImg : function(imgUrl, __callback, target,param){
        cc.loader.loadImg(imgUrl, { isCrossOrigin: false }, function (err, texture) {
            try {
                if (err) {
                    if(__callback)
                        __callback(target,false,undefined,param);
                    return;
                }

                var sprite = new cc.Sprite(texture);
                sprite.anchorX = 0;
                sprite.anchorY = 0;
                sprite.x = 0;
                sprite.y = 0;

                //获取成功回调
                if(__callback) {
                    __callback(target, true, sprite,param);
                }
            }catch (e)
            {
                if(__callback)
                    __callback(target,false,undefined,param);
            }

        });
    },
};
